'use strict';
app.controller('modulesGoodsCtrl',['$scope','$stateParams','$state','modulesGoodsService','utilService','toaster',function($scope,$stateParams,$state,modulesGoodsService,utilService,toaster){
    $scope.id=$stateParams.id;

    //查询模块商品
    $scope.modulesGoodsListVO={
        modulesId:$stateParams.id
    };

    $scope.findPageAll=function(){
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain + 'modules/goods',
            params:$scope.modulesGoodsListVO
        }).then(function(result){
        });
    }

    $scope.findPageAll();


    //移除模块商品
    $scope.delModulesGoods= function (id) {
        modulesGoodsService.delModulesGoods($scope,id).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.findPageAll();
                    break;
                case 655:
                    toaster.pop('waiting', "操作失败", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }

    $scope.abledState=true;

    //上移模块商品
    $scope.sortDown= function (id) {
        if(!$scope.abledState){
            return;
        }
        $scope.abledState=false;

        modulesGoodsService.sortDown($scope,id).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.findPageAll();
                    break;
                case 656:
                    toaster.pop('waiting', "操作失败", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
            $scope.abledState=true;
        })
    }

    //上移
    $scope.sortUp= function (id) {
        if(!$scope.abledState){
            return;
        }
        $scope.abledState=false;

        modulesGoodsService.sortUp($scope,id).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.findPageAll();
                    break;
                case 657:
                    toaster.pop('waiting', "操作失败", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
            $scope.abledState=true;
        })
    }

    //置顶
    $scope.sortStick= function (id) {
        if(!$scope.abledState){
            return;
        }
        $scope.abledState=false;
        modulesGoodsService.sortStick($scope,id).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.findPageAll();
                    break;
                case 655:
                    toaster.pop('waiting', "操作失败", result.message);
                    break;
                case 657:
                    toaster.pop('waiting', "操作失败", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
            $scope.abledState=true;
        })
    }

    //上下架
    $scope.updateStatus= function (id) {
        if(!$scope.abledState){
            return;
        }
        $scope.abledState=false;
        modulesGoodsService.updateStatus($scope,id).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.findPageAll();
                    break;
                case 655:
                    toaster.pop('waiting', "操作失败", result.message);
                    break;
                case 657:
                    toaster.pop('waiting', "操作失败", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
            $scope.abledState=true;
        })
    }


    //批量上下架
    $scope.batchUpdateStatus= function (status) {

        if(!$scope.abledState){
            return;
        }
        $scope.abledState=false;
        modulesGoodsService.batchUpdateStatus($scope,status).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.findPageAll();
                    break;
                case 655:
                    toaster.pop('waiting', "操作失败", result.message);
                    break;
                case 657:
                    toaster.pop('waiting', "操作失败", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
            $scope.abledState=true;
        })
    }



    //批量选择
    $scope.selected=[];

    $scope.isChecked = function(item){
        return $scope.selected.indexOf(item) >= 0 ;
    } ;

    $scope.updateSelection = function($event,item){

        var checkbox = $event.target ;
        var checked = checkbox.checked ;

        if(checked){
            $scope.selected.push(item) ;
        }else{
            var idx = $scope.selected.indexOf(item) ;
            $scope.selected.splice(idx,1) ;
        }
    };

    $scope.isCheckedAll=function(){

        if($scope.list==null){
            return;
        }

        return $scope.selected.length==$scope.list.length;
    }

    $scope.checkAll=function($event){
        var checkbox = $event.target ;
        var checked = checkbox.checked ;

        for(var i=0;i<$scope.list.length;i++){
            if(checked){
                $scope.selected.push($scope.list[i]);
            }else{
                $scope.selected.splice(0,$scope.selected.length);
            }
        }
    }

    $scope.initSelect=function(){
        $scope.selected=[];
    }


}])